Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils.network.hosts: Make subclass of Host instantiable #5714

Merged
merged 1 commit into from
Jul 7, 2023

Conversation

yanglei-rh
Copy link
Contributor

@yanglei-rh yanglei-rh commented Jul 3, 2023

A regression bug was introduced by 94fb7a8,which results Host's subclass LocalHost can not be instantiated. Existing instantiation methods the same as "localhost = LocalHost()" will no longer fail after changed.

ID:2219366
Signed-off-by: Lei Yang [email protected]

@yanglei-rh
Copy link
Contributor Author

@luckyh @huangyum Could you please help me review it? Thanks in advance.

@huangyum
Copy link

huangyum commented Jul 4, 2023

The changes look good to me, however, I'd prefer a clearer description in commit summary and message.

@@ -48,7 +48,7 @@ class Host:
"""

def __init__(self, host):
if isinstance(self, Host):
if type(self) == Host:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, there is another approach to avoid performing this check by invoking type directly, that is we could make the Host be an abstract class with its __init__ method being an abstract method.

$ cat /tmp/t.py 
from abc import ABCMeta, abstractmethod


class Foo(metaclass=ABCMeta):

    @abstractmethod
    def __init__(self, host):
        self.host = host


class Bar(Foo):

    def __init__(self, host='localhost'):
        super().__init__(host)


b = Bar()
print(b.host)
f = Foo('localhost')


$ python3 /tmp/t.py 
localhost
Traceback (most recent call last):
  File "/tmp/t.py", line 19, in <module>
    f = Foo('localhost')
        ^^^^^^^^^^^^^^^^
TypeError: Can't instantiate abstract class Foo with abstract method __init__

Copy link
Contributor

@luckyh luckyh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@yanglei-rh yanglei-rh changed the title hosts.LocalHost: fix a regression bug hosts.Host: fix a regression bug Jul 4, 2023
@yanglei-rh
Copy link
Contributor Author

Hello @luckyh @huangyum Could you please help me review it again? I have changed the commit summary and message. Thanks in advance.

@yanglei-rh
Copy link
Contributor Author

Hello @clebergnu Could you please help me review this patch? Thanks in advance.

@yanglei-rh yanglei-rh changed the title hosts.Host: fix a regression bug utils.network.hosts: Make subclass of Host instantiable Jul 4, 2023
Copy link

@huangyum huangyum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@yanglei-rh
Copy link
Contributor Author

Test Pass:
(1/1) Host_RHEL.m9.u3.ovmf.qcow2.virtio_scsi.up.virtio_net.Guest.RHEL.9.3.0.x86_64.io-github-autotest-qemu.expose_host_mtu.q35: PASS (113.74 s)

@clebergnu clebergnu self-assigned this Jul 6, 2023
Copy link
Contributor

@clebergnu clebergnu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @clebergnu Could you please help me review this patch? Thanks in advance.

@yanglei-rh the change itself looks good. I apologize introducing the breakage.

In order to silence pylint, and make CI checks pass, please add:

diff --git a/avocado/utils/network/hosts.py b/avocado/utils/network/hosts.py
index 0a1c2add3..7d61ad640 100644
--- a/avocado/utils/network/hosts.py
+++ b/avocado/utils/network/hosts.py
@@ -48,7 +48,7 @@ class Host:
     """
 
     def __init__(self, host):
-        if type(self) == Host:
+        if type(self) == Host:  # pylint: disable=C0123
             raise TypeError("Host class should not be instantiated")
         self.host = host
 

@yanglei-rh
Copy link
Contributor Author

Hello @clebergnu Could you please help me review this patch? Thanks in advance.

@yanglei-rh the change itself looks good. I apologize introducing the breakage.

In order to silence pylint, and make CI checks pass, please add:

diff --git a/avocado/utils/network/hosts.py b/avocado/utils/network/hosts.py
index 0a1c2add3..7d61ad640 100644
--- a/avocado/utils/network/hosts.py
+++ b/avocado/utils/network/hosts.py
@@ -48,7 +48,7 @@ class Host:
     """
 
     def __init__(self, host):
-        if type(self) == Host:
+        if type(self) == Host:  # pylint: disable=C0123
             raise TypeError("Host class should not be instantiated")
         self.host = host
 

Thanks for your help review. After silence pylint, CI checks pass.

@yanglei-rh
Copy link
Contributor Author

Hi @luckyh Could you please help me review it again and merge this MR? Thanks a lot.

@PraveenPenguin
Copy link
Collaborator

Hi @luckyh Could you please help me review it again and merge this MR? Thanks a lot.

@yanglei-rh can you please address CI issue , as have space around #pylint comment

https://github.com/avocado-framework/avocado/actions/runs/5481979456/jobs/9989576970?pr=5714#step:8:39

A regression bug was introduced by 94fb7a8,which results Host's subclass
LocalHost can not be instantiated. Existing instantiation methods the same as
"localhost = LocalHost()" will no longer fail after changed.

Signed-off-by: Lei Yang <[email protected]>
@yanglei-rh
Copy link
Contributor Author

Hi @luckyh Could you please help me review it again and merge this MR? Thanks a lot.

@yanglei-rh can you please address CI issue , as have space around #pylint comment

https://github.com/avocado-framework/avocado/actions/runs/5481979456/jobs/9989576970?pr=5714#step:8:39

Hi @PraveenPenguin Thanks for your reminder, this problem has been fixed, please help review it again, thanks a lot.

@clebergnu clebergnu merged commit 40b75f1 into avocado-framework:master Jul 7, 2023
57 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants